package com.example.software.controller;

import com.example.software.entity.CompanyInfo;
import com.example.software.service.CompanyInfoService;
import com.example.software.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/company")
public class CompanyController {
    
    private static final Logger log = LoggerFactory.getLogger(CompanyController.class);

    @Autowired
    private CompanyInfoService companyInfoService;

    /**
     * 获取企业信息
     * @param request HTTP请求
     * @return 企业信息
     */
    @GetMapping("/info")
    public Result<CompanyInfo> getCompanyInfo(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String userType = (String) request.getAttribute("userType");
        
        // 验证用户类型
        if (!"company".equals(userType)) {
            return Result.error("非企业用户无权访问企业信息");
        }
        
        CompanyInfo companyInfo = companyInfoService.getByUserId(userId);
        return Result.success(companyInfo);
    }

    /**
     * 更新企业信息
     * @param companyInfo 企业信息
     * @param request HTTP请求
     * @return 更新结果
     */
    @PutMapping("/info")
    public Result<Boolean> updateCompanyInfo(@RequestBody CompanyInfo companyInfo, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String userType = (String) request.getAttribute("userType");
        
        // 验证用户类型
        if (!"company".equals(userType)) {
            return Result.error("非企业用户无权更新企业信息");
        }
        
        // 设置用户ID，确保只能更新自己的信息
        companyInfo.setUserId(userId);
        
        boolean result = companyInfoService.updateCompanyInfo(companyInfo);
        return Result.success(result);
    }
} 